package cn.wildfire.chat.app.main;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sourcemuch.im.caiba.R;import com.sourcemuch.im.caiba.R2;
import com.sourcemuch.im.caiba.R2;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.OnClick;
import cn.wildfire.chat.app.extendsion.classes.ResponseEntity;
import cn.wildfire.chat.app.extendsion.classes.ResponsePhoneNumberUserInfo;
import cn.wildfire.chat.app.extendsion.classes.SendCodeRequest;
import cn.wildfire.chat.app.extendsion.classes.ViewTimerSet;
import cn.wildfire.chat.app.extendsion.https.OkHttpRequest;
import cn.wildfire.chat.app.extendsion.https.ReqCallBack;
import cn.wildfire.chat.app.extendsion.https.ServerUrlClient;
import cn.wildfire.chat.app.extendsion.ui.ToastTip;
import cn.wildfire.chat.app.extendsion.utils.TextUtils;
import cn.wildfire.chat.kit.WfcBaseActivity;

/**
 * @author Lizhong
 */
public class RecoveryBySMSActivity extends WfcBaseActivity {

    ResponsePhoneNumberUserInfo responsePhoneNumberUserInfo;
    @BindView(R2.id.tv_selectedCountryTxt)
    TextView tvSelectedCountryTxt;
    @BindView(R2.id.et_phoneNumber)
    EditText etPhoneNumber;
    @BindView(R2.id.et_authCodeEditText)
    EditText etAuthCodeEditText;
    @BindView(R2.id.tv_requestAuthCodeButton)
    TextView tvRequestAuthCodeButton;

    private String countryName;

    @Override
    protected void afterViews() {
        super.afterViews();

        Intent intent = getIntent();
        countryName = intent.getStringExtra("CountryName");
        responsePhoneNumberUserInfo = intent.getParcelableExtra("UserInfo");
        responsePhoneNumberUserInfo.getResult().getMobile();

        tvSelectedCountryTxt.setText(countryName);
        etPhoneNumber.setText(responsePhoneNumberUserInfo.getResult().getMobile());
        etPhoneNumber.setFocusable(false);
        etPhoneNumber.setClickable(false);


    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_recovery_by_sms;
    }

    @Override
    protected void setTitleCenterThenSetSearchView() {
        super.setTitleCenterThenSetSearchView();
        setToolbarOrReplaceType(true, 2);
        setReplaceTitle(null, R.string.title_recovery_by_sms);
        setSearchLayoutVisibility(false);
    }

    @OnClick({R2.id.tv_requestAuthCodeButton, R2.id.btn_next_step})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tv_requestAuthCodeButton) {
            getAuthCode();
        } else if (id == R.id.btn_next_step) {
            doNext();
        }
    }

    private void doNext() {
        String trim = etAuthCodeEditText.getText().toString().trim();
        if (TextUtils.isTextEmpty(trim)) {
            ToastTip.tip(getString(R.string.verification_can_not_empty));
            return;
        }
        HashMap<String, Object> params = new HashMap<>();
        params.put("code", trim);
        params.put("mobile", responsePhoneNumberUserInfo.getResult().getMobile());
        params.put("regionId", responsePhoneNumberUserInfo.getResult().getRegionId());

        OkHttpRequest.getInstance(this).requestAsynJsonObject(ServerUrlClient.REQUEST_SMS, params, new ReqCallBack<Object>() {
            @Override
            public void onReqSuccess(Object result) {
                if (!TextUtils.isTextEmpty(result.toString())) {
                    try {
                        ResponseEntity<String, String> responseEntity = new ResponseEntity<>(result.toString());
                        if (responseEntity.isSuccess()) {
                            ToastTip.tip(getString(R.string.verification_success));
                            Intent intent = new Intent(RecoveryBySMSActivity.this, SetNewPasswordActivity.class);
                            intent.putExtra("UserInfo", responsePhoneNumberUserInfo);
                            startActivity(intent);

                        } else {
                            ToastTip.tip(responseEntity.message);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                Toast.makeText(RecoveryBySMSActivity.this, "请求失败!", Toast.LENGTH_SHORT).show();
            }
        }).request();
    }

    /**
     * 倒计时对象
     */
    private ViewTimerSet viewTimerSet;

    private void getAuthCode() {
        try {
            if (viewTimerSet != null && !viewTimerSet.isTimeoutFinished()) {
                return;
            }
            String phoneNumber = etPhoneNumber.getText().toString();
            if (TextUtils.isTextEmpty(phoneNumber)) {
                Toast.makeText(this, "请输入手机号!", Toast.LENGTH_SHORT).show();
                return;
            }
            viewTimerSet = new ViewTimerSet(RecoveryBySMSActivity.this, tvRequestAuthCodeButton);
            viewTimerSet.run();
            new SendCodeRequest(RecoveryBySMSActivity.this, phoneNumber, "" + responsePhoneNumberUserInfo.getResult().getRegionId(), new SendCodeRequest.OnSendCodeListener() {
                @Override
                public void send(boolean success) {
                    if (!success) {
                        viewTimerSet.forceInterrupt();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
